﻿using System;

using System.Collections.Generic;

using System.Linq;

using System.Web;

using System.Web.UI;

using System.Web.UI.WebControls;

using Exhibition.Dao;

using Exhibition.Service;

using Exhibition.Common.Helper;

using Exhibition.Dao.ViewModel;
using System.Text;

namespace Exhibition.Manage
{

    public partial class UserDetail : BaseAdminPage
    {

     public   UserModel model = new UserModel();
        protected void Page_Load(object sender, EventArgs e)
        {

            if (!IsPostBack)
            {
                hfId.Value = reqID.ToString();
                GetUser();
                GetUCode();
                GetPrope();
            }

        }

        private void GetPrope()
        {
            StringBuilder sb = new StringBuilder();
            List<CodesModel> cdms = CenterService.codesService.GetModels(4);


            foreach (var c in cdms)
            {
                string strchk = string.Empty;
                if (model != null)
                {
                    if (!string.IsNullOrEmpty(model.Properties))
                    {
                        if (model.Properties.Contains(c.ECode))
                        {
                            strchk = " checked='checked'";
                        }
                    }
                }

                sb.AppendFormat("<input type='radio' name='rdoprope' value='{0}' {2}/>{1}&nbsp;", c.ECode, c.EName, strchk);
            }
            ltrRdo.Text = sb.ToString();
        }


        private void GetUCode()
        {
            StringBuilder sb = new StringBuilder();
            List<CodesModel> cdms = CenterService.codesService.GetModels(2);


            foreach (var c in cdms)
            {
                string strchk = string.Empty;
                if (model != null)
                {
                    if (!string.IsNullOrEmpty(model.ContactType))
                    {
                        if (model.ContactType.Contains(c.ECode))
                        {
                            strchk = " checked='checked' ";
                        }
                    }
                }

                sb.AppendFormat("<input type='checkbox' name='chkct' value='{0}' {2}/>请{1}联系我&nbsp;", c.ECode, c.EName, strchk);
            }
            ltrContact.Text = sb.ToString();

        }
        private void GetUser()
        {

            model = CenterService.userService.GetOneById(reqID);
            if (model != null)
            {


                //用户名
                txtUserName.Value = model.UserName;

                //密码
                txtPassWord.Value = model.PassWord;

                //公司名称
                txtCompName.Value = model.CompName;
                
                //性别(0保密1男2女)
                drpList.SelectedValue = model.Sex.ToString();

                //邮箱
                txtEmail.Value = model.Email;

                //手机
                txtMobile.Value = model.Mobile;
                rdoNation.SelectedValue = model.Nationality.ToString();
                hfLevel.Value = model.UserLevel.ToString() ;

                //区域父ID
                //txtAreaPid.Value = model.AreaPid.ToString();

                //区域子ID
                //txtAreaId.Value = model.AreaId.ToString();

                //真实姓名
                txtRealName.Value = model.RealName; 
 

                //来源
                txtInCome.Value = model.InCome;

                //地址
                txtUserAddress.Value = model.UserAddress;

                drpType.SelectedValue = model.UserType.ToString();


                ltrSore.Text = model.UserLevel == null ? "0" : model.UserLevel.ToString();

              

                //是否验证为真实用户
                //txtIsCheck.Value = model.IsCheck.ToString();

             

                //
                //txtUserLevel.Value = model.UserLevel.ToString();

                //认可联系方式
                //txtContactType.Value = model.ContactType.ToString();

            }

        }

        /// <summary>
        /// 保存一条数据
        /// <summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        protected void btnSave_Click(object sender, EventArgs e)
        {

            model = string.IsNullOrEmpty(hfId.Value)||hfId.Value=="0" ? new UserModel() : CenterService.userService.GetOneById(Convert.ToInt32(hfId.Value));
            //用户名
            model.UserName = txtUserName.Value;
            if (!string.IsNullOrEmpty(txtPassWord.Value))
            {
                //密码
                model.PassWord =StringUtils.EncryptPassWord( txtPassWord.Value,StringUtils.PassFormat.MD5);
            }

            //公司名称
            model.CompName = txtCompName.Value;

            //性别(0保密1男2女)
            model.Sex = Convert.ToInt32(drpList.SelectedValue);

            //邮箱
            model.Email = txtEmail.Value;

            //手机
            model.Mobile = txtMobile.Value;

            //区域父ID
            model.AreaPid = 0;

            //区域子ID
            model.AreaId = 0;
            model.IsCheck = 1;

            //真实姓名
            model.RealName = txtRealName.Value;

            //用户国籍
            model.Nationality =rdoNation.SelectedValue ;

            //性质
            model.Properties = Request["rdoprope"];

            //创建时间
            model.CreateTime = DateTime.Now;

            //来源
            model.InCome = txtInCome.Value;

            //地址
            model.UserAddress = txtUserAddress.Value;

            //是否删除(0正常1删除)
            model.IsDelete = 0;

            //用户类别（1观众2展商）
            model.UserType =Convert.ToInt32( drpType.SelectedValue);

            //网站ID
            model.SiteID = CurrentSite.Id;

            //
            model.CreateUserId = AdminUser.Id;
            model.UserType = 2;
            //
            model.IsMember = Convert.ToInt32(chkIsMember.Checked) ;
            model.UserLevel =Convert.ToInt32( hfLevel.Value);
            ////
            //model.Position = txtPosition.Value;

            ////
            //model.Depart = txtDepart.Value;

            //是否验证为真实用户
           // model.IsCheck = Convert.ToInt32(txtIsCheck.Value);

            //邮件激活码
            //model.verifycode = txtverifycode.Value;

            //
            //model.UserLevel = Convert.ToInt32(txtUserLevel.Value);

            //认可联系方式
            model.ContactType = Request["chkct"]; 

            if (CenterService.userService.Save(model) > 0)
            {
                JsHelper.Unblock(this,"恭喜你数据保存成功！");
               
            }

        }

    }

}

